import api from "../../api/api"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myProfit: 0,
    myProfitRate: 0.2,
    myFundList: [],
    showAddFundDialog:false,
  },

  // 点击添加基金
  addFundClick(){
    this.setData({
      showAddFundDialog:true
    })
  },

  // 点击修改份额
  fundItemClick(e){
    let fundId = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    let count = e.currentTarget.dataset.count;
    console.log(fundId);
    console.log(name);
    console.log(count);
  },

  async getMyFundList() {
  
    let myFundList = [{
      fundId: "004070",
      count: "100"//份额
    },{
      fundId: "161024",
      count: "100"
    }];

    let myProfit = 0;
    for (let index = 0; index < myFundList.length; index++) {
      let data = await api.request.getFundData(true, myFundList[index].fundId);
      myFundList[index].name = data.name;
      myFundList[index].todayRate = data.gszzl;
      myFundList[index].todayMoney = (data.gszzl * myFundList[index].count / 100).toFixed(2);
      console.log(myFundList[index].todayMoney);
      myProfit = (parseFloat(myProfit) + parseFloat(myFundList[index].todayMoney)).toFixed(2);
    }
    if (myProfit < 0) {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#37d049',
        animation: {
          duration: 400,
          timingFunc: 'easeIn'
        }
      })
    } else {
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#e43f24',
        animation: {
          duration: 400,
          timingFunc: 'easeIn'
        }
      })
    }
    console.log(myFundList);
    this.setData({
      myFundList: myFundList,
      myProfit: myProfit,
    })
  },

  // 获取单个基金的当日详细信息
  getFundData(fundId) {
    api.request.getFundData(true, fundId).then(data => {
      console.log(data);
    }).catch(err => {
      api.toast(err)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyFundList();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})